package com.lphl.pet.fragment;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.lphl.pet.R;
import com.lphl.pet.activity.ChoosePetActivity;
import com.lphl.pet.activity.DistanceDetectionActivity;
import com.lphl.pet.activity.FactoryResetActivity;
import com.lphl.pet.activity.PetTargetActivity;
import com.lphl.pet.activity.PicListActivity;
import com.lphl.pet.activity.UpDataActivity;
import com.lphl.pet.util.BatteryDialogUtil;
import com.lphl.pet.util.MenuDialogUtil;

public class MenuFragment extends Fragment implements OnClickListener {

    private View view;
    private TextView tv_electric_quantity; // 电量
    private TextView tv_connect; // 连接
    private TextView menu_info;
    private TextView menu_setting;
    private TextView menu_pic;
    private TextView menu_monitoring;
    private TextView menu_update;
    private TextView menu_restore;
    private TextView menu_move;
    private TextView menu_delete;
    private BatteryReceiver receiver;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        receiver = new BatteryReceiver();
        IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        getActivity().registerReceiver(receiver, filter); //注册BroadcastReceiver
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.layout_menu_right, null);
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        initView();
        super.onActivityCreated(savedInstanceState);
    }

    private void initView() {
        tv_electric_quantity = (TextView) view.findViewById(R.id.tv_electric_quantity);
        tv_connect = (TextView) view.findViewById(R.id.tv_connect);

        menu_info = (TextView) view.findViewById(R.id.menu_info);
        menu_setting = (TextView) view.findViewById(R.id.menu_setting);
        menu_pic = (TextView) view.findViewById(R.id.menu_pic);
        menu_monitoring = (TextView) view.findViewById(R.id.menu_monitoring);
        menu_update = (TextView) view.findViewById(R.id.menu_update);
        menu_restore = (TextView) view.findViewById(R.id.menu_restore);
        menu_move = (TextView) view.findViewById(R.id.menu_move);
        menu_delete = (TextView) view.findViewById(R.id.menu_delete);
        menu_info.setOnClickListener(this);
        menu_setting.setOnClickListener(this);
        menu_pic.setOnClickListener(this);
        menu_monitoring.setOnClickListener(this);
        menu_update.setOnClickListener(this);
        menu_restore.setOnClickListener(this);
        menu_move.setOnClickListener(this);
        menu_delete.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Intent intent = null;
        switch (v.getId()) { // 宠物信息
            case R.id.menu_info:
                Toast.makeText(getActivity(), "宠物信息", Toast.LENGTH_LONG).show();
                intent = new Intent(getActivity(), ChoosePetActivity.class);
                startActivity(intent);
                break;

            case R.id.menu_setting: // 设置目标
                intent = new Intent(getActivity(), PetTargetActivity.class);
                startActivity(intent);
                break;

            case R.id.menu_pic: // TA图库
                intent = new Intent(getActivity(), PicListActivity.class);
                startActivity(intent);
                break;

            case R.id.menu_monitoring: // 监控距离
                intent = new Intent(getActivity(), DistanceDetectionActivity.class);
                startActivity(intent);
                break;

            case R.id.menu_update: // 更新
                intent = new Intent(getActivity(), UpDataActivity.class);
                startActivity(intent);
                break;

            case R.id.menu_restore: // 恢复
                intent = new Intent(getActivity(), FactoryResetActivity.class);
                startActivity(intent);
                break;

            case R.id.menu_move: // 解除绑定
                MenuDialogUtil.getInstance().showUnbindDialog(getActivity());
                break;

            case R.id.menu_delete: // 删除账号
                MenuDialogUtil.getInstance().showDeleteDialog(getActivity());
                break;

            default:
                break;
        }
    }

    @Override
    public void onDestroy() {
        // 取消广播监听
        getActivity().unregisterReceiver(receiver);
    }

    /**
     * 监听当前手机电量的广播
     */
    public class BatteryReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (Intent.ACTION_BATTERY_CHANGED.equals(intent.getAction())) {
                Bundle bundle = intent.getExtras();
                // 获取当前电量
                int current = bundle.getInt("level");
                // 获取总电量
                int total = bundle.getInt("scale");
                tv_electric_quantity.setText(current * 100 / total + "%");
            }
        }
    }
}
